<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta name="baidu-site-verification" content="UqlC4pwKIm">
  <meta name="baidu-site-verification" content="d3U0dGeqGw">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">



  <meta name="google-site-verification" content="9Ho8iwjF7bDNkvQpVQHlhFPHnsfuMWlsOvTTgEPIuy4">














  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
      
    

    
  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Roboto Slab:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|Lobster Two:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  







<link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="https://cdn.jsdelivr.net/gh/lopyfrontend/common@0.0.1/icon/favicon-180x180.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="https://cdn.jsdelivr.net/gh/lopyfrontend/common@0.0.1/icon/favicon-32x32.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="https://cdn.jsdelivr.net/gh/lopyfrontend/common@0.0.1/icon/favicon-16x16.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="network,develop,">





  <link rel="alternate" href="/atom.xml" title="码奴假娃" type="application/atom+xml">






<meta name="description" content="作为一个开发者，日常工作就是解决各类开发问题。但是，由于众所周知的原因，有些问题的难度不在于问题本身，而在于其它的东西">
<meta name="keywords" content="network,develop">
<meta property="og:type" content="article">
<meta property="og:title" content="openvpn通过ss隐藏流量">
<meta property="og:url" content="https://blog.lopygo.com/2020/2020-03-06-openvpn-over-ss/index.html">
<meta property="og:site_name" content="码奴假娃">
<meta property="og:description" content="作为一个开发者，日常工作就是解决各类开发问题。但是，由于众所周知的原因，有些问题的难度不在于问题本身，而在于其它的东西">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2025-09-16T17:33:20.683Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="openvpn通过ss隐藏流量">
<meta name="twitter:description" content="作为一个开发者，日常工作就是解决各类开发问题。但是，由于众所周知的原因，有些问题的难度不在于问题本身，而在于其它的东西">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":true},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: 'undefined',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.lopygo.com/2020/2020-03-06-openvpn-over-ss/">





  <title>openvpn通过ss隐藏流量 | 码奴假娃</title>
  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-136884610-2', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?52567a573db99b54cccb66af4c41822b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">码奴假娃</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">裸皮狗</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br>
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.lopygo.com/2020/2020-03-06-openvpn-over-ss/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Tony Java Z">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="码奴假娃">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">openvpn通过ss隐藏流量</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-03-06T11:54:22+00:00">
                2020-03-06
              </time>
            

            
              <span class="post-meta-divider">|</span>
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-check-o"></i>
              </span>
              
                <span class="post-meta-item-text">更新于&#58;</span>
              
              <time title="更新于" itemprop="dateModified" datetime="2025-09-16T17:33:20+00:00">
                2025-09-17
              </time>
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/develop/" itemprop="url" rel="index">
                    <span itemprop="name">develop</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2020/2020-03-06-openvpn-over-ss/" class="leancloud_visitors" data-flag-title="openvpn通过ss隐藏流量">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2,030
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  8
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <script src="/assets/js/APlayer.min.js"> </script><p>作为一个开发者，日常工作就是解决各类开发问题。但是，由于众所周知的原因，有些问题的难度不在于问题本身，而在于其它的东西<br><a id="more"></a>  </p>
<h1 id="科学上网"><a href="#科学上网" class="headerlink" title="科学上网"></a>科学上网</h1><ol>
<li><p>浏览器上网问题（比如google一些资料啊），一般各种科学上网工具都都解决</p>
</li>
<li><p>一些软件需要外网，但是又没有配置代理的功能</p>
<p> 可以考虑用<code>ss</code>的全局代理或<code>proxifier</code>等工具</p>
</li>
<li><p>还有极少数的软件，用以上两种办法都没法解决的<br> 比如使用 go get</p>
<p> 通常，go get 可以在 <code>GO111MODULE</code>为<code>on</code>时，使用代理。但是在使用 <a href="https://github.com/go-flutter-desktop/go-flutter" target="_blank" rel="noopener">go-flutter</a>时，要求不能设为<code>on</code></p>
<p> 然后我尝试了各种方法，在win下面都不能成功正常使用（linux下却go get成功了），也不知道是不是win本身的问题</p>
</li>
</ol>
<h1 id="vpn"><a href="#vpn" class="headerlink" title="vpn"></a>vpn</h1><p>因为众所周知的原因，市面上各大vpn会被轻易的识别出来，因为vpn一般的不会隐藏流量。所以我们要学会用一些方法来隐藏一下流量，比如用利用隧道</p>
<p>以<a href="#openvpn安装">openvpn</a>为例</p>
<p>正常情况</p>
<p>openvpn client <-> openvpn server</-></p>
<p>用隧道的情况</p>
<p>openvpn client <-> 隧道入口 <-> 隧道服务（ss/ssh等） <-> 隧道出口 <-> openvpn server</-></-></-></-></p>
<p>即 client 和 server 之间，利用隧道来隐藏流量</p>
<h2 id="ssh隧道"><a href="#ssh隧道" class="headerlink" title="ssh隧道"></a>ssh隧道</h2><p>ssh隧道是很常见的一种方式，只是在下用得少</p>
<p>安全性肯定没得说，能否被某系统识别这就不知道了，毕竟用得少</p>
<h2 id="ss隧道"><a href="#ss隧道" class="headerlink" title="ss隧道"></a>ss隧道</h2><p>我以前看过ss的wiki，发现有一个配置是也是讲openvpn的，用的是openvpn的内置代理<code>socks-proxy</code>，但是我试了几次都没成功，发现代理不生效。并且我把本的ss关了后，opv仍然能连接。。。</p>
<p>然后，思索了下，虽然没成功，也算有点灵感</p>
<p>ss-libev里面，有一个<code>ss-tunnel</code>就是专门用于端口转发的，那么我们可以用他来做个隧道。然后通过这个隧道来建立openvpn的c/s的连接</p>
<p>ps：笔者试了openvpn如果用udp方式连接，那么这种方式不成功，不知道是不是哪里的配置不小心搞错了。以下步骤是基于openvpn使用tcp方式连接</p>
<p>下面说一下具体步骤</p>
<h3 id="1、先确定本地的ss-client-能正常连接服务端的-ss-server"><a href="#1、先确定本地的ss-client-能正常连接服务端的-ss-server" class="headerlink" title="1、先确定本地的ss client 能正常连接服务端的 ss server"></a>1、先确定本地的ss client 能正常连接服务端的 ss server</h3><p>由于这个前面已经写过了，这里就不用写了</p>
<a href="/2015/08-11-p-shadowsocks-config/" title="shadowsocks常用配置">shadowsocks常用配置</a>
<h3 id="2、用ss-tunnel建立一个隧道"><a href="#2、用ss-tunnel建立一个隧道" class="headerlink" title="2、用ss-tunnel建立一个隧道"></a>2、用ss-tunnel建立一个隧道</h3><p><a href="#ss-tunnel编译">ss-tunel编译</a></p>
<p>下载好ss-tunnel，并在同一文件夹下建一个配置文件 <code>config.json</code><br><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">"server"</span>: <span class="string">"SS_SERVER_IP"</span>,</span><br><span class="line">  <span class="attr">"server_port"</span>: <span class="number">8388</span>,</span><br><span class="line">  <span class="attr">"local_port"</span>: <span class="number">1194</span>,</span><br><span class="line">  <span class="attr">"password"</span>: <span class="string">"PASSWORD"</span>,</span><br><span class="line">  <span class="attr">"method"</span>: <span class="string">"aes-256-cfb"</span>,</span><br><span class="line">  <span class="attr">"tunnel_address"</span>: <span class="string">"172.17.0.1:1194"</span>,</span><br><span class="line">  <span class="attr">"mode"</span>: <span class="string">"tcp_and_udp"</span>,</span><br><span class="line">  <span class="attr">"timeout"</span>: <span class="number">5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>比如在win中，目录结构如下</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">├─ config.json</span><br><span class="line">├─ run.bat</span><br><span class="line">├─ ss-tunnel-x64.exe</span><br></pre></td></tr></table></figure>
<p>其中run.bat中，就一句代码</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.\ss-tunnel-x64.exe -c .\config.json</span><br></pre></td></tr></table></figure>
<p>将配置文件的设设置正确，与ss客户端的配置大致相同</p>
<p>其中：</p>
<p><code>local_port</code>：本地监听端口，也就是上文提到的隧道入口的端口</p>
<p><code>tunnel_address</code>: 目标地址和端口，即上文提到的隧道出口的地址和端口</p>
<p>示例中，因为我的<code>openvpn</code>和<code>ss-server</code>在同一台机器上，并且都是用docker跑的，所以用 172.17.0.1(用docker内网也可以，但是用docker内置dns则不能，原因未明)</p>
<h3 id="3、在命令行中运行"><a href="#3、在命令行中运行" class="headerlink" title="3、在命令行中运行"></a>3、在命令行中运行</h3><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">.\ss-tunnel-x64.exe -c .\config.json</span><br></pre></td></tr></table></figure>
<p>或者直接运行目录中的<code>run.bat</code></p>
<p>隧道建立成功后，我们在本地访问 <code>local_port</code> (localhost:local_port)，就会访问到相对于<code>ss-server</code>服务端的<code>tunnel_address</code></p>
<p>以示例配置文件为<code>config.json</code>例，访问本地的<code>1194</code>端口时，ss-tunnel会将请求转发到服务端，并从服务端访问 <code>172.17.0.1:1194</code></p>
<h3 id="4、修改openvpn的配置文件"><a href="#4、修改openvpn的配置文件" class="headerlink" title="4、修改openvpn的配置文件"></a>4、修改openvpn的配置文件</h3><p>openvpn的配置文件为 xxxx.ovpn</p>
<p>修改你的ovpn配置文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">remote 127.0.0.1 1194 tcp</span><br><span class="line">route SS_SERVER_IP 255.255.255.255 net_gateway</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>通常情况，一般为直接连接，则 <code>remote</code> 后面通常为一个公网ip，比如我把vpn搭在和ss-server搭在同一台电脑上，则这里的ip就和ss-server的ip一致。</p>
<p>现在，因为用到的隧道，就应该填隧道的ip和端口，以示例<code>config.json</code>为例，<code>就填 remote 127.0.0.1 1194 tcp</code><br>(示例中，ss-tunnel和openvpn在同一台机器上，如果不同机，则以实际为准)</p>
<p>然后，就是还要加上一句 <code>route XXX_IP 255.255.255.255 net_gateway</code></p>
<p>这句的意思是 <code>XXX_IP</code>的流量，用直连方式，不通过vpn。这句很重要的，因为vpn是全局的，默认情况ss的连接也会走vpn，如果不排除，则ss也不能连接到远程服务器了</p>
<p>另因为我的ss-client和openvpn-client在同一机器，所以排除的是ss-client连接的服务端地址</p>
<p>如果ss-client和openvpn-client 不在同一机器，则应该排除的是ss-client的ip地址</p>
<h3 id="5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了"><a href="#5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了" class="headerlink" title="5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了"></a>5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了</h3><h3 id="6、优点"><a href="#6、优点" class="headerlink" title="6、优点"></a>6、优点</h3><ol>
<li>全局，比proxifier的全局更彻底</li>
<li>用ss的加密隧道隐藏了openvpn的流量，使之相对不易被封</li>
<li>这种方式不用将openvpn的服务端的端口暴露（监听0.0.0.0），以示例来说，openvpn 只需监听172.17.0.1:1194就行了</li>
</ol>
<h3 id="7、缺点"><a href="#7、缺点" class="headerlink" title="7、缺点"></a>7、缺点</h3><p>肯定比较慢一点</p>
<h1 id="其它"><a href="#其它" class="headerlink" title="其它"></a>其它</h1><h2 id="ss-tunnel编译"><a href="#ss-tunnel编译" class="headerlink" title="ss-tunnel编译"></a>ss-tunnel编译</h2><blockquote>
<p>详情见<br><a href="https://github.com/shadowsocks/shadowsocks-libev" target="_blank" rel="noopener">https://github.com/shadowsocks/shadowsocks-libev</a></p>
</blockquote>
<p>我一般在linux下编译，跨平台编译依赖docker</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/shadowsocks/shadowsocks-libev</span><br><span class="line"></span><br><span class="line"><span class="built_in">cd</span> shadowsocks-libev/docker/mingw</span><br><span class="line"></span><br><span class="line">git submodule init &amp;&amp; git submodule update</span><br><span class="line"></span><br><span class="line">make</span><br></pre></td></tr></table></figure>
<h2 id="openvpn安装"><a href="#openvpn安装" class="headerlink" title="openvpn安装"></a>openvpn安装</h2><p>一般习惯用docker了，然后用docker-compose编排</p>
<p>先上一个<code>docker-compose.yml</code>文件</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">version:</span> <span class="string">'2.1'</span></span><br><span class="line"><span class="attr">services:</span></span><br><span class="line">  <span class="attr">server:</span></span><br><span class="line">    <span class="attr">container_name:</span> <span class="string">'openvpn'</span></span><br><span class="line">    <span class="attr">image:</span> <span class="string">'kylemanna/openvpn'</span></span><br><span class="line">    <span class="attr">volumes:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">openvpn_data:/etc/openvpn</span></span><br><span class="line">    <span class="attr">ports:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="number">1194</span><span class="string">:1194</span></span><br><span class="line">    <span class="attr">privileged:</span> <span class="literal">true</span></span><br><span class="line">    <span class="attr">networks:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">frontend</span></span><br><span class="line">    <span class="attr">restart:</span> <span class="string">always</span></span><br><span class="line">    <span class="attr">env_file:</span></span><br><span class="line">      <span class="bullet">-</span> <span class="string">.env</span></span><br><span class="line"><span class="attr">volumes:</span></span><br><span class="line">  <span class="attr">openvpn_data:</span></span><br><span class="line">    <span class="attr">external:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">ovpn-data-example</span></span><br><span class="line"><span class="attr">networks:</span></span><br><span class="line">  <span class="attr">frontend:</span></span><br><span class="line">    <span class="attr">external:</span></span><br><span class="line">      <span class="attr">name:</span> <span class="string">frontend</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>其中的network和 volume 是个人习惯，即会先在外部用docker创建</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker network create frontend</span><br><span class="line">docker volume create --name ovpn-data-example</span><br></pre></td></tr></table></figure>
<h3 id="步骤"><a href="#步骤" class="headerlink" title="步骤"></a>步骤</h3><h4 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment"># 先准备</span></span><br><span class="line"><span class="comment"># 随便找一个文件夹再创建一个openvpn文件夹。比如 ~/openvpn</span></span><br><span class="line"></span><br><span class="line">mkdir ~/openvpn &amp;&amp; <span class="built_in">cd</span> ~/openvpn</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个上面的 docker-compose.yml 文件。</span></span><br><span class="line"></span><br><span class="line">cat &lt;&lt;eof &gt; docker-compose.yml</span><br><span class="line">version: <span class="string">'2.1'</span></span><br><span class="line">services:</span><br><span class="line">  server:</span><br><span class="line">    container_name: <span class="string">'openvpn'</span></span><br><span class="line">    image: <span class="string">'kylemanna/openvpn'</span></span><br><span class="line">    volumes:</span><br><span class="line">      - openvpn_data:/etc/openvpn</span><br><span class="line">    ports:</span><br><span class="line">      - 1194:1194</span><br><span class="line">    privileged: <span class="literal">true</span></span><br><span class="line">    networks:</span><br><span class="line">      - frontend</span><br><span class="line">    restart: always</span><br><span class="line">    env_file:</span><br><span class="line">      - .env</span><br><span class="line">volumes:</span><br><span class="line">  openvpn_data:</span><br><span class="line">    external:</span><br><span class="line">      name: ovpn-data-example</span><br><span class="line">networks:</span><br><span class="line">  frontend:</span><br><span class="line">    external:</span><br><span class="line">      name: frontend</span><br><span class="line">eof</span><br><span class="line"></span><br><span class="line"><span class="comment"># 新建 .env文件，写入相关信息</span></span><br><span class="line"><span class="comment"># 这一步我们定义两个变量,OPV_HOST 和 OPV_SCHEME</span></span><br><span class="line"><span class="comment"># OPV_HOST指你的公网ip，或域名</span></span><br><span class="line"><span class="comment"># OPV_SCHEME指opvpn的协议，openvpn支持 tcp 和udp. 本文中，在测试udp转发时失败了，所以用tcp</span></span><br><span class="line"></span><br><span class="line">cat &lt;&lt;eof &gt; .env</span><br><span class="line">OPV_HOST=www.baidu.commmmm</span><br><span class="line">OPV_SCHEME=tcp</span><br><span class="line">eof</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 准备工作做完了，下面开始</span></span><br></pre></td></tr></table></figure>
<h4 id="运行一个容器并配置"><a href="#运行一个容器并配置" class="headerlink" title="运行一个容器并配置"></a>运行一个容器并配置</h4><ol>
<li><p>运行一个容器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose run --rm server bash</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ovpn_genconfig -u <span class="variable">$&#123;OPV_SCHEME&#125;</span>://<span class="variable">$&#123;OPV_HOST&#125;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>初始化</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">touch /etc/openvpn/vars</span><br><span class="line">ovpn_initpki</span><br></pre></td></tr></table></figure>
<p>该步骤会填写相关信息</p>
<p>Enter PEM pass phrase: 输入123456（你是看不见的）<br>Verifying - Enter PEM pass phrase: 输入123456（你是看不见的）<br>Common Name (eg: your user, host, or server name) [Easy-RSA CA]:回车一下<br>Enter pass phrase for /etc/openvpn/pki/private/ca.key:输入123456 </p>
</li>
<li><p>创建用户<br>命令中的 <code>CLIENTNAME</code> 为你的客户端用户名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">easyrsa build-client-full CLIENTNAME nopass</span><br><span class="line">Enter pass phrase <span class="keyword">for</span> /etc/openvpn/pki/private/ca.key:输入123456 （你是看不见的）</span><br></pre></td></tr></table></figure>
</li>
<li><p>生成密钥<br>生成的密钥因为需要使用并且还要传给客户端，所以退出容器操作比较方便</p>
<p>按下 <code>ctrl + d</code> ，即可退出容器</p>
<p>执行以下命令</p>
<p>命令中的 <code>CLIENTNAME</code> 为你的客户端用户名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose run --rm server ovpn_getclient CLIENTNAME &gt; CLIENTNAME.ovpn</span><br></pre></td></tr></table></figure>
</li>
<li><p>将生成的 <code>CLIENTNAME.ovpn</code> 发送到客户端以供使用</p>
</li>
<li><p>运行openvpn服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker-compose up -d</span><br></pre></td></tr></table></figure>
<p>自此，openvpn服务就能成功运行了</p>
</li>
</ol>

      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    Tony Java Z
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://blog.lopygo.com/2020/2020-03-06-openvpn-over-ss/" title="openvpn通过ss隐藏流量">https://blog.lopygo.com/2020/2020-03-06-openvpn-over-ss/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/network/" rel="tag"># network</a>
          
            <a href="/tags/develop/" rel="tag"># develop</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/2020-01-15-full-remote-develop/" rel="next" title="一个完整的远程开发环境">
                <i class="fa fa-chevron-left"></i> 一个完整的远程开发环境
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2020/2020-03-07-compile-vscode-for-web/" rel="prev" title="从源码编译vscode，用于web端vscode">
                从源码编译vscode，用于web端vscode <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
      <div id="sidebar-dimmer"></div>
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="Tony Java Z">
            
              <p class="site-author-name" itemprop="name">Tony Java Z</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

			<!--my custom code begin-->
			<script src="https://cdn.jsdelivr.net/npm/jquery@3.1.0/dist/jquery.min.js"></script>
			<script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.0/velocity.min.js"></script>
			<script type="text/javascript">
			  $("#sidebar").hover(function(){
				$("#mydivshow").velocity('stop').velocity({opacity: 1});
			  },function(){
				$("#mydivshow").velocity('stop').velocity({opacity: 0});
			  });
			</script>
			<div id="mydivshow" class="mydivshow">
			<!--my custom code end-->
			
          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">118</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">17</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">104</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/uljjmhn520" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:284141050 @qq.com" target="_blank" title="Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>Mail</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=pZedkZSRlJWQleXU1IvGysg" target="_blank" title="QQ邮我">
                    
                      <i class="fa fa-fw fa-envelope"></i>QQ邮我</a>
                </span>
              
            
          </div>

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                友情链接
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lopygo.com" title="主站" target="_blank">主站</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://blog.csdn.net/u011475210" title="WordZzzCsdn" target="_blank">WordZzzCsdn</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://wordzzzz.github.io" title="WordZzzGitPage" target="_blank">WordZzzGitPage</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.choudalao.com" title="臭大佬" target="_blank">臭大佬</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://blog.wingflare.com" title="翼闪的奶嘴" target="_blank">翼闪的奶嘴</a>
                  </li>
                
              </ul>
            </div>
          

          
        </div>
		<!--my custom code begin-->
		</div>
		<!--my custom code end-->
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#科学上网"><span class="nav-number">1.</span> <span class="nav-text">科学上网</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#vpn"><span class="nav-number">2.</span> <span class="nav-text">vpn</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#ssh隧道"><span class="nav-number">2.1.</span> <span class="nav-text">ssh隧道</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ss隧道"><span class="nav-number">2.2.</span> <span class="nav-text">ss隧道</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、先确定本地的ss-client-能正常连接服务端的-ss-server"><span class="nav-number">2.2.1.</span> <span class="nav-text">1、先确定本地的ss client 能正常连接服务端的 ss server</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、用ss-tunnel建立一个隧道"><span class="nav-number">2.2.2.</span> <span class="nav-text">2、用ss-tunnel建立一个隧道</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、在命令行中运行"><span class="nav-number">2.2.3.</span> <span class="nav-text">3、在命令行中运行</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、修改openvpn的配置文件"><span class="nav-number">2.2.4.</span> <span class="nav-text">4、修改openvpn的配置文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了"><span class="nav-number">2.2.5.</span> <span class="nav-text">5、将修改后的ovpn文件重新导入openvpn-client中，正常情况下，就能连接了</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6、优点"><span class="nav-number">2.2.6.</span> <span class="nav-text">6、优点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#7、缺点"><span class="nav-number">2.2.7.</span> <span class="nav-text">7、缺点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其它"><span class="nav-number">3.</span> <span class="nav-text">其它</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#ss-tunnel编译"><span class="nav-number">3.1.</span> <span class="nav-text">ss-tunnel编译</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#openvpn安装"><span class="nav-number">3.2.</span> <span class="nav-text">openvpn安装</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#步骤"><span class="nav-number">3.2.1.</span> <span class="nav-text">步骤</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#准备工作"><span class="nav-number">3.2.1.1.</span> <span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#运行一个容器并配置"><span class="nav-number">3.2.1.2.</span> <span class="nav-text">运行一个容器并配置</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2017 &mdash; <span itemprop="copyrightYear">2025</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Tony Java Z</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">72.9k</span>
  
</div>






  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a></div>




        
<div class="busuanzi-count">
  
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script src="https://cdn.jsdelivr.net/npm/jquery@2.1.3/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fastclick@1.0.6/lib/fastclick.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-lazyload@1.9.7/jquery.lazyload.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.2.2/velocity.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@1.2.2/velocity.ui.min.js"></script>




  
  <script src="https://cdn.jsdelivr.net/npm/jquery-fancybox@2.0.0/source/js/jquery.fancybox.pack.js"></script>



  
  <script src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1.0.0/canvas-nest.min.js"></script>












  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>




  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("", "");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
  </script>

  

  
  


  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.3"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.3"></script>


  
  <script type="text/javascript" src="/js/src/exturl.js?v=5.1.3"></script>



  
  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>

<!-- 页面点击小红心 -->
<script type="text/javascript" src="/js/src/love.js"></script>


<script>
  console.log('%c本Blog仅用于个人学习',"color:orange; background:blue; font-size: 16pt");

  console.log('%c内容中有部分资源引用自互联网',"color:orange; background:blue; font-size: 16pt");

  console.log('%c如果不小心侵犯了您的权益，请联系  284141050@qq.com 处理相关信息',"color:orange; background:blue; font-size: 16pt");

</script>

</body>
</html>
